﻿#region Using
using System;
using System.IO;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using JigLibX.Physics;
using JigLibX.Geometry;
using JigLibX.Collision;
using JigLibX.Math;
#endregion Using

namespace LloydGame
{
    public class SmokeParticleSystem : ParticleSystem
    {
        #region Constructor
        public SmokeParticleSystem(Game p_game, int p_nrParticles, Vector3 p_position, bool p_respawnParticles)
            : base(p_game, p_nrParticles, p_position, p_respawnParticles)
        {
            m_particleTexture = Game.Content.Load<Texture2D>("Textures\\Particle_Smoke");

            foreach (Particle p in m_particles)
                p.energy = (float)((Game1)Game).random.NextDouble() * 25;
        }
        #endregion

        #region Update
        protected override void updateParticle(int p_nr, GameTime p_gameTime)
        {
            float dt = ((float)p_gameTime.ElapsedRealTime.Milliseconds) / 1000.0f;

            m_particles[p_nr].position += m_particles[p_nr].velocity * dt;

            m_particles[p_nr].energy -= dt;
        }

        protected override void setupShape(int p_nr)
        {
            Vector3 particleViewPos = Vector3.Transform(m_particles[p_nr].position, ((Game1)Game).camera.view);

            Color color = new Color(0.35f, 0.35f, 0.35f
                , (m_particles[p_nr].energy > 5 ? 1 : m_particles[p_nr].energy / 5) * 0.5f);

            // setup shape vertices in view space
            m_shapes[p_nr * 4].Position =
                new Vector3(-m_particles[p_nr].size, m_particles[p_nr].size, 0) + particleViewPos;
            m_shapes[p_nr * 4].Color = color;

            m_shapes[p_nr * 4 + 1].Position =
                new Vector3(m_particles[p_nr].size, m_particles[p_nr].size, 0) + particleViewPos;
            m_shapes[p_nr * 4 + 1].Color = color;

            m_shapes[p_nr * 4 + 2].Position =
                new Vector3(m_particles[p_nr].size, -m_particles[p_nr].size, 0) + particleViewPos;
            m_shapes[p_nr * 4 + 2].Color = color;

            m_shapes[p_nr * 4 + 3].Position =
                new Vector3(-m_particles[p_nr].size, -m_particles[p_nr].size, 0) + particleViewPos;
            m_shapes[p_nr * 4 + 3].Color = color;
        }

        protected override void setParticleDefaults(Particle p_particle, Vector3 p_particleDir)
        {
            p_particle.position = new Vector3(m_spawnPosition.X, m_spawnPosition.Y, m_spawnPosition.Z);

            float x = (float)(((Game1)Game).random.NextDouble() * 2 - 1);
            float y = (float)((Game1)Game).random.NextDouble();
            float z = (float)(((Game1)Game).random.NextDouble() * 2 - 1);

            Vector3 v = new Vector3(x, y, z);
            v.Normalize();

            v.Y *= 0.5f;

            p_particle.velocity = v;

            p_particle.energy = 25;
            p_particle.size = 2f;
        }
        #endregion
    }
}
